package com.example.cloudmusic.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.cloudmusic.databinding.ItemBannerContentBinding
import com.example.cloudmusic.home.model.Banner

/**
 * 轮播图适配器
 */
class BannerAdapter : BaseListAdapter<Banner, BannerAdapter.ViewHolder>(BannerDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemBannerContentBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class ViewHolder(private val binding: ItemBannerContentBinding) : 
        RecyclerView.ViewHolder(binding.root) {
        
        init {
            binding.root.setOnClickListener { onItemClick(adapterPosition) }
        }

        fun bind(item: Banner) {
            Glide.with(binding.root)
                .load(item.imageUrl)
                .into(binding.cmIvBanner)
        }
    }

    private class BannerDiffCallback : DiffUtil.ItemCallback<Banner>() {
        override fun areItemsTheSame(oldItem: Banner, newItem: Banner): Boolean =
            oldItem.id == newItem.id

        override fun areContentsTheSame(oldItem: Banner, newItem: Banner): Boolean =
            oldItem.imageUrl == newItem.imageUrl
    }
} 